package vertx;

import io.vertx.core.Vertx;
import io.vertx.core.eventbus.EventBus;
import io.vertx.core.eventbus.MessageConsumer;
import netty.util.StartAndStop;
import org.apache.commons.lang3.RandomUtils;
import test.PerformanceAnalyze;

public class BusBridgeServerSingleInstance extends StartAndStop {

    public void start() throws InterruptedException {
        PerformanceAnalyze performanceAnalyze = new PerformanceAnalyze();

        Vertx vertx = Vertx.vertx();

        EventBus eb = vertx.eventBus();


        int size = 100000;
        String address = "news.uk.sport";
        for (int i = 0; i < size; i++) {
            MessageConsumer<Long> consumer = eb.consumer(address + i);
            consumer.handler(message -> {
                performanceAnalyze.delay(System.nanoTime() - message.body());
                performanceAnalyze.meter();
            });
            consumer = eb.consumer(address + i);
            consumer.handler(message -> {
                performanceAnalyze.meter();
            });
        }


        while (running) {
            Thread.sleep(1000);
            for (int i = 0; i < size; i++) {
                eb.publish(address + RandomUtils.nextInt(0, size), System.nanoTime());
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {


    }
}
